<?php

declare(strict_types=1);

namespace App\ChannelCode\Controller\Dashboard\Group;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\RequestMapping;
use App\Common\Middleware\DashboardAuthMiddleware;
use Base\Action\AbstractAction;
use App\ChannelCode\Service\ChannelCodeGroupService;
use Hyperf\HttpServer\Contract\RequestInterface;

/**
 * 渠道码分组列表.
 *
 * Class Index
 * @Controller
 */
class Index extends AbstractAction
{
    /**
     * @Inject
     * @var ChannelCodeGroupService
     */
    private $channelCodeGroup;


    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/channelCodeGroup/index", methods="get")
     */
    public function handle()
    {
        $corp_id = $this->request->input('userInfo.corp_id');
        $res = $this->channelCodeGroup
            ->getChannelCodeGroupsByCorpId([$corp_id], ['id', 'name']);

        if (empty($res)) {
            return [];
        }

        array_walk($res, function (&$item) {
            $item['groupId'] = $item['id'];
            unset($item['id']);
        });

        return $res;
    }
}
